package tcln.gxtx.client.utils;

import tcln.gxtx.client.utils.assertion.GwtAssert;

import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * A simple delegator which delegates handling code for all uncaught exceptions to an {@link AsyncCallback} instance.
 * 
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class UncaughtExceptionCallback implements UncaughtExceptionHandler {
    private AsyncCallback<Void> callback;

    /**
     * @param callback
     */
    public UncaughtExceptionCallback(AsyncCallback<Void> callback) {
        GwtAssert.ARGUMENTS.notNull(callback, "A callback is missing");
        this.callback = callback;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onUncaughtException(Throwable e) {
        callback.onFailure(e);
    }
}
